<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="11">
            <el-form-item label="组名" prop="group.id"
                :rules="[
                 ]">
          <SelectTree
                      ref="group"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"
                      url="/info/dev/jfInfodevGroup/treeData"
                      :value="inputForm.group.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.group.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="11">
            <el-form-item label="设备名" prop="name"
                :rules="[
                  {required: true, message:'设备名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="设备名对应终端设备"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11">
            <el-form-item label="位置" prop="place"
                :rules="[
                 ]">
              <el-input v-model="inputForm.place" placeholder="请填写位置"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11">
            <el-form-item label="同步配置" prop="sync"
                :rules="[
                 ]">
                <el-select v-model="inputForm.sync" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('sync_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="MAC地址" prop="mac"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mac" placeholder="请填写MAC地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="IP地址" prop="ip"
                :rules="[
                 ]">
              <el-input v-model="inputForm.ip" placeholder="请填写IP地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="温度" prop="temperature"
                :rules="[
                 ]">
              <el-input v-model="inputForm.temperature" placeholder="请填写温度"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="内存" prop="memory"
                :rules="[
                 ]">
              <el-input v-model="inputForm.memory" placeholder="请填写内存"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="硬盘容量" prop="disk"
                :rules="[
                 ]">
              <el-input v-model="inputForm.disk" placeholder="请填写硬盘容量"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="软件版本" prop="version"
                :rules="[
                 ]">
              <el-input v-model="inputForm.version" placeholder="请填写软件版本"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="总内存" prop="memoryTotal"
                :rules="[
                 ]">
              <el-input v-model="inputForm.memoryTotal" placeholder="请填写总内存"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="总硬盘容量" prop="diskTotal"
                :rules="[
                 ]">
              <el-input v-model="inputForm.diskTotal" placeholder="请填写总硬盘容量"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="分辨率" prop="resolution"
                :rules="[
                 ]">
              <el-input v-model="inputForm.resolution" placeholder="请填写分辨率"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="最近上线" prop="onlineDate"
                :rules="[
                 ]">
                <el-date-picker
                      v-model="inputForm.onlineDate"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="播放节目" prop="task.id"
                :rules="[
                 ]">
          <SelectTree
                      ref="task"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"

                      url="/info/program/jfInfoTask/treeData"
                      :value="inputForm.task.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.task.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="开机时间" prop="startupTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.startupTime" placeholder="请填写开机时间"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="关机时间" prop="shutdownTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.shutdownTime" placeholder="请填写关机时间"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="11" v-show="method ==='view'">
            <el-form-item label="系统" prop="os"
                :rules="[
                 ]">
                <el-select v-model="inputForm.os" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('os_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          group: {
            id: ''
          },
          name: '',
          mac: '',
          ip: '',
          temperature: '',
          memory: '',
          disk: '',
          version: '',
          memoryTotal: '',
          diskTotal: '',
          resolution: '',
          onlineDate: '',
          task: {
            id: ''
          },
          startupTime: '',
          shutdownTime: '',
          sync: '',
          os: '',
          place: ''
        }
      }
    },
    components: {
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建信发设备`
        } else if (method === 'edit') {
          this.title = '修改信发设备'
        } else if (method === 'view') {
          this.title = '查看信发设备'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/info/dev/jfInfodev/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfInfodev)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/info/dev/jfInfodev/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
